package gemini.castor.ui.client.page.widget.categorypreview;

import gemini.basic.model.ProductType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;

public class CategoryPreviewWidget extends Composite implements HasHandlers{

	@UiTemplate("CategoryPreviewWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, CategoryPreviewWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public CategoryPreviewWidget() {
		initWidget(binder.createAndBindUi(this));
	}

	@UiField
	Image imgCategory;
	
	@UiField
	HTML htmName;
	
	@UiHandler("imgCategory")
	void onClickImage(ClickEvent e){
		fireEvent(new CategoryPreviewEvent(CategoryPreviewEvent.ForwardType.SELECT));
	}
	
	public void loadData(ProductType productType){
		imgCategory.setUrl(productType.getImageUrl());
		htmName.setHTML(productType.getName());
	}
	
	public void addCategoryPreviewHandler(CategoryPreviewHandler handler){
		this.addHandler(handler, CategoryPreviewEvent.getType());
	}
}
